package config;

import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.mvc.Controller;

public class UnKnowHander extends AbstractHandlerMapping
{

	private String ext="";
	
	private String templateLoaderPath;

	public void setTemplateLoaderPath(String templateLoaderPath)
	{
		this.templateLoaderPath = templateLoaderPath;
	}

	@Override
	protected Object getHandlerInternal(HttpServletRequest request) throws Exception
	{
		String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
		String path = lookupPath.replaceAll(ext+"$", "");
		URL url = request.getServletContext().getResource(templateLoaderPath + path + ".ftl");
		if (url == null)
		{
			return null;
		}
		Controller controller = new Controller()
		{
			@Override
			public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
			{
				ModelAndView modelAndView = new ModelAndView(path);
				return modelAndView;
			}
		};

		HandlerMethod hm = new HandlerMethod(controller, controller.getClass().getMethod("handleRequest", HttpServletRequest.class, HttpServletResponse.class));
		return hm;
	}

	public String getExt() {
		return ext;
	}

	public void setExt(String ext) {
		this.ext = ext;
	}

	
}
